// page/coachs/student/index.js
var utils = require("../../utils/util.js")
Page({
  data: {
    isHide: false,
    navIndex: 1,
    msg: {
      icon: "/images/icon/nodata.png",
      desc: "暂无数据"
    },
    startDate: "",
    endDate: "",
    topTabItems: ["在学学员(56)", "毕业学员(45)"],
    currentTopItem: 0,
    list: [],
    isShow: false,
    showIndex: "",
    keywords: "",
  },
  onLoad: function (options) {
    var that = this
    console.log(that.data.startDate)
    console.log(that.data.endDate)
    getApp().getUserOpenId(
      function (uid) {
        that.request()
      },
      function (info) {

      },
    );
  },

  // 网络请求
  request: function () {
    var that = this
    console.log(that.data.startDate)
    console.log(that.data.endDate)
    wx.request({
      url: getApp().globalData.host + "&m=Coach&a=studentList", //仅为示例，并非真实的接口地址
      data: {
        uid: getApp().globalData.uid,
        s_time: that.data.startDate,
        e_time: that.data.endDate,
        keywords: that.data.keywords,
      },
      header: {
        'content-type': 'application/json', // 默认值
      },
      method: 'POST',
      success: function (res) {
        console.log(res.data)
        //请求成功
        if (res.data.status == 1) {
          console.log(res.data)
          for(var i =0;i<res.data.list1.length;i++){
            if (res.data.list1[i].time_out==1){
              res.data.list1[i].status = "超时"
            }else{
              res.data.list1[i].status = ""
            }
          }
          for (var i = 0; i < res.data.list2.length; i++) {
            if (res.data.list2[i].graduate == 1) {
              res.data.list2[i].status = "毕业"
            } else {
              res.data.list2[i].status = ""
            }
          }
          that.setData({
            isHide: true,
            studying: res.data.list1,
            graduation: res.data.list2,
            topTabItems: ["在学学员(" + res.data.list1_num + ")", "毕业学员(" + res.data.list2_num + ")"]
          })
          that.setData({
            list: that.data.studying
          })
        } else {//请求失败
          wx.showToast({
            title: res.data.info,
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: "请求失败",
        })
      }
    })
  },

  bindKeyInput: function (e) {
    var that = this
    this.setData({
      keywords: e.detail.value
    })
    that.request()
  },


  //切换顶部标签
  switchTab: function (e) {
    var idx = e.currentTarget.dataset.idx
    this.setData({
      currentTopItem: idx
    })
    this.switchItems(idx)
  },
  //swiperChange
  bindChange: function (e) {
    var idx = e.detail.current
    this.setData({
      currentTopItem: idx
    })
    this.switchItems(idx)
  },
  switchItems: function (index) {
    wx.showLoading({
      title: '正在加载',
    })
    if (index == 0) {
      //在学学员
      this.setData({
        list: this.data.studying
      })
    } else {
      //毕业学员
      this.setData({
        list: this.data.graduation
      })
    }
    wx.hideLoading()
  },
  makePhoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: this.data.list[e.currentTarget.dataset.idx].phone //仅为示例，并非真实的电话号码
    })
  },
  loadMoreData: function () {
    //加载更多
    wx.showLoading({
      title: '正在加载…',
    })
    var that = this
    var newData = []  //新数据
    if (that.data.currentTopItem == 0) {
      //在学学员
      that.setData({
        list: that.data.studying
      })
    } else {
      //毕业学员
      that.setData({
        list: that.data.graduation
      })
    }
    wx.hideLoading()
  },
  bindStartDate: function (e) {
    var that = this
    this.setData({
      date1: e.detail.value,
      startDate: e.detail.value
    })
    that.request()
  },
  bindEndDate: function (e) {
    var that = this
    this.setData({
      date2: e.detail.value,
      endDate: e.detail.value
    })
    that.request()
  },
  showDrop: function (e) {
    this.setData({
      isShow: true,
      showIndex: e.currentTarget.dataset.idx
    })
  },
  closeDrop: function () {
    this.setData({
      isShow: false,
      showIndex: ""
    })
  }
})